﻿<?php 
class Log {

	/**
	 * 写日志
	 * @access public
	 * @return boolean
	 */
	public static function write($log){
		$log_file = Log::isBak();
		$fh = fopen($log_file,'ab');
		fwrite($fh,date('Y-m-d H:i:s').' '.$log."\r\n");
		fclose($fh);
	}

	/**
	 * 判断是否需要备份
	 * @access public
	 * @return $log_file  log日志的路径
	 */
	public static function isBak(){
		
		$log_file = './log.txt';

		#判断文件是否存在
		if( !file_exists($log_file) ){
			touch($log_file);
			return $log_file;
		}

		#判断文件大小
		if( filesize($log_file) < 1024*1024 )return $log_file;

		#超出1M，备份文件
		if( !Log::Bak() ){
			return $log_file;
		} else {
			touch($log_file);
			return $log_file;
		};
	}

	/**
	 * 备份日志
	 * @access public
	 * @return $log_file
	 */
	public static function Bak(){
		$log_file = './log.txt';
		$new_file = './'.date('Ymd').mt_rand(1000,9999).'bak_log.txt';
		return rename($log_file,$new_file);
	}
}
	# 创建随机文件名
	$dir = '../../data/wx_xml/';
	is_dir($dir) || mkdir($dir,0777,true);
	$file = time().rand(1000,9999).'.xml';

	$xml_name = $dir.$file;

	# 获得微信的参数，并写到本地
	$xml = file_get_contents('php://input');
	file_put_contents($xml_name, $xml);
	Log::write($xml.'  '.date('Y-m-d H:i:s'));

	# 拼接url地址
	$server_addr = $_SERVER['SERVER_ADDR']=='::1'?'localhost':$_SERVER['SERVER_NAME'];
    $url = 'http://'.$server_addr.ltrim(dirname($_SERVER['SCRIPT_NAME']),'\\');
    $url = str_replace('api/wxpay','',$url);
	# 调用处理的函数
//	$res = file_get_contents($url.'index.php/api/Zhifu/wx_zhifu_huidiao?xml_name='.$file);
	$url = $url.'index.php/api/Zhifu/wx_zhifu_huidiao?xml_name='.$file;
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
	$dxycontent = curl_exec($ch);
echo '<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>';
 ?>